# -*- coding: utf-8 -*-
# @Date    : 2015-06-21 15:50:35
# @Author  : Your Name (you@example.org)
# @Link    : http://example.org
# @Version : $Id$

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.admin.user.student import Student
from web.models.admin.user.staff import Staff
import json
import time
import md5
import base64
from lib import libupload


@route(r'/admin/user/staff/?', name='')
class IndexHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        school = self.get_cookie('school', base64.b16encode('{}'))
        school = json.loads(base64.b16decode(school))
        dt = school.get('staff', {})
        staff_name = dt.get('staff_name', '')
        staff_dorm = dt.get('staff_dorm', '')
        staff_position = dt.get('staff_position', '')

        where = {
            "Fname":   staff_name,
            "Fdorm_id": staff_dorm,
            "Fposition": staff_position,
        }

        page = (int(cur_page) - 1) * self.offset

        drom = yield Student().select_dorm(school_id)
        ret, count = yield Staff().select_staff(school_id, where, page, self.offset)

        page_info = {'total': count, 'page': int(cur_page)}

        self.render("admin/user/staff/list.html", data=ret,
                    page_info=page_info, drom=drom, where=where)

    def post(self):
        cur_page = self.get_argument('page', default='1')
        school_id = self.get_argument('schoolId', default=self.school_id)
        staff_name = self.get_argument('staff_name', default='')
        staff_dorm = self.get_argument('staff_dorm', default='')
        staff_position = self.get_argument('staff_position', default='')

        school = self.get_cookie('school', base64.b16encode('{}'))
        school = json.loads(base64.b16decode(school))
        dt = school.get('staff', {})

        if staff_name != '':
            dt['staff_name'] = staff_name
        else:
            dt.pop('staff_name', '')

        if staff_dorm != '':
            dt['staff_dorm'] = staff_dorm
        else:
            dt.pop('staff_dorm', '')

        if staff_position != '':
            dt['staff_position'] = staff_position
        else:
            dt.pop('staff_position', '')

        school["staff"] = dt
        self.set_cookie('school', base64.b16encode(json.dumps(school)))

        self.redirect('/admin/user/staff/')


@route(r'/admin/user/staff/new/?')
class NewHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)

        drom = yield Student().select_dorm(school_id)

        self.render("admin/user/staff/new.html", drom=drom)

    @asynchronous
    @gen.coroutine
    def post(self):
        ltype = self.get_argument('ltype', default='0')
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fage = self.get_argument('Fage', default='0')
        Fhire_date = self.get_argument('Fhire_date', default='')
        Ftel = self.get_argument('Ftel', default='')
        Fposition = self.get_argument('Fposition', default='0')
        Fdorm_id = self.get_argument('Fdorm_id', default='0')
        Favater = self.get_argument('Favater', default='')
        Favater = self.request.files.get('Favater')

        if Favater is not None:
            Favater, path = libupload.save(Favater[0])
            libupload.img.thumbnail(path, path, 100, 100)
        else:
            Favater = ""

        staff_info_data = {
            "Fname":         Fname,
            "Fage":           Fage,
            "Fhire_date":      Fhire_date,
            "Ftel":         Ftel,
            "Fschool_id":   school_id,
            "Favater":          Favater,
            "Fposition":          Fposition,
            "Fdorm_id":    Fdorm_id,
            "Fcreate_time":  time.time(),
            "Favater":       Favater
        }

        count = yield Staff().insert_staff(staff_info_data)

        if ltype == '0':
            self.redirect('/admin/user/staff/')
        else:
            self.redirect('/admin/user/staff/new/')


@route(r'/admin/user/staff/del/(?P<FstaffId>[0-9].*)/(?P<cur_page>[0-9].*)', name='staff.del_staff')
class DelHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, FstaffId, cur_page):
        school_id = self.get_argument('schoolId', default=self.school_id)

        ret = Staff().del_staff(FstaffId)

        self.redirect('/admin/user/staff/?page=' + cur_page)


@route(r'/admin/user/staff/edit/(?P<Fid>[0-9].*)', name='staff.edit')
class EditHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)

        staff = yield Staff().find_staff(Fid)
        drom = yield Student().select_dorm(school_id)

        self.render("admin/user/staff/edit.html", drom=drom, staff=staff)

    @asynchronous
    @gen.coroutine
    def post(self, Fid):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fname = self.get_argument('Fname', default='')
        Fage = self.get_argument('Fage', default='0')
        Fhire_date = self.get_argument('Fhire_date', default='')
        Ftel = self.get_argument('Ftel', default='')
        Fposition = self.get_argument('Fposition', default='0')
        Fdorm_id = self.get_argument('Fdorm_id', default='0')
        Favater = self.get_argument('Favater', default='')
        Favater = self.request.files.get('Favater')

        if Favater is not None:
            Favater, path = libupload.save(Favater[0])
            libupload.img.thumbnail(path, path, 100, 100)
        else:
            Favater = ""

        staff_info_data = {
            "Fname":         Fname,
            "Fage":           Fage,
            "Fhire_date":      Fhire_date,
            "Ftel":         Ftel,
            "Fschool_id":   school_id,
            "Favater":          Favater,
            "Fposition":          Fposition,
            "Fdorm_id":    Fdorm_id,
            "Fcreate_time":  time.time()
        }
        if Favater != "":
            staff_info_data['Favater'] = Favater

        count = yield Staff().update_staff(Fid, staff_info_data)

        self.redirect('/admin/user/staff/')


@route(r'/admin/user/staff/result/?')
class ResultHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    def get(self):
        print self.get_argument('param', default=1)
        self.write('asdfsadf')
